package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.domain.Product;
import ssm.mapper.ProductMapper;

import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private ProductMapper productMapper;

    @RequestMapping("/productList")
    public String index(Model model){
        List<Product> all = productMapper.findAll();
        model.addAttribute("proList",all);
        return "productList";
    }

    //跳转添加产品列表
    @RequestMapping("/addProduct")
    public String toAddProduct(){
        return "addProduct";
    }

    //添加
    @RequestMapping("/addPro")
    public String addProduct(Product product){
        productMapper.add(product);
        return "forward:/productList";
    }

    //删除
    @RequestMapping("/deletePro")
    public String delete(Integer id){
        productMapper.delete(id);
        return "forward:/productList";
    }

    //修改
    @RequestMapping("/toUpdatePro")
    public String toUpdate(Integer id,Model model){
        Product one = productMapper.findOne(id);
        model.addAttribute("onePro",one);
        return "editProduct";
    }
    @RequestMapping("/updatePro")
    public String update(Product product){
        productMapper.update(product);
        return "forward:/productList";
    }
}
